
//  LaunchAnimationTool.m
//  Gunterapple
//
//  Created by 热带雨林 on 2021/1/1.
//  Copyright © 2021 热带雨林. All rights reserved.
//

#import "LaunchAnimationTool.h"

static NSInteger const imageViewsborderOffset = 150;

@implementation LaunchAnimationTool

+ (void)showLaunchAnimationToWindowView{
    
    //直接使用旧方法 [[[UIApplication sharedApplication] delegate] window] 来获取,旧项目中，如果没有使用SceneDelegate
    UIWindow *window =  [[[UIApplication sharedApplication] delegate] window];
    if([[[UIApplication sharedApplication] delegate] window]){
        //已经获得window
    }else{
        //但iOS13后，苹果为了兼容分屏功能，加入了SceneDelegate
        if(@available (iOS 13.0, *)){
            //如果是普通的APP，可以使用
            //      SceneDelegate * delegate = (SceneDelegate *)windowScene.delegate;
            //       UIWindow * mainWindow = delegate.window;
            NSArray *array = [[[UIApplication sharedApplication] connectedScenes] allObjects];
            UIWindowScene *windowScene = (UIWindowScene *)array[0];
            //由于在sdk开发中，引入不了scenedelegate的头文件，所以需要使用KVC获取宿主app的window
            window = [windowScene valueForKeyPath:@"delegate.window"];
            if(window){
                //
            }else{
                window = [UIApplication sharedApplication].windows.lastObject;
            }
        }
    }
    UIView *launchView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, GDScreenW, GDScreenH)];
    [window addSubview:launchView];
    UIImageView *backImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, GDScreenW + imageViewsborderOffset, GDScreenH)];
    backImageView.image = [UIImage imageNamed:@"fx_launchimage_bg"];
    backImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    backImageView.contentMode = UIViewContentModeScaleAspectFill;
    [launchView addSubview:backImageView];
    
    UIImage *iconImage = [UIImage imageNamed:@"fx_launchimage_front"];
    UIImageView *iconImageView = [[UIImageView alloc]initWithImage:iconImage];
    iconImageView.frame = GDScreenBounds;
    [launchView addSubview:iconImageView];
    

    [UIView animateWithDuration:3.6f delay:0.0f options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveLinear animations:^{
          
        NSInteger randomTranslationValueX = imageViewsborderOffset - 100;
        NSInteger randomTranslationValueY = 0;
      
        CGAffineTransform translationTransform = CGAffineTransformMakeTranslation(randomTranslationValueX, randomTranslationValueY);
      
        CGFloat randomScaleTransformValue = 1.0f;
      
  
        CGAffineTransform scaleTransform = CGAffineTransformMakeScale(randomScaleTransformValue, randomScaleTransformValue);
        
        backImageView.transform = CGAffineTransformConcat(scaleTransform, translationTransform);
        
    } completion:^(BOOL finished) {
        [launchView removeFromSuperview];
    }];
}


//#pragma mark - 适配IOS13
//- (UIWindow *)getCurrentWindow{
//
//    if([[[UIApplication sharedApplication] delegate] window]){
//        return [[[UIApplication sharedApplication] delegate] window];
//    }else{
//
//        if(@available(iOS 13.0,*)){
//            NSArray *array = [[[UIApplication sharedApplication] connectedScenes] allObjects];
//            //强转
//            UIWindowScene *windowScene = (UIWindowScene *)array[0];
//            //如果是普通的APP，可以使用
//            //      SceneDelegate * delegate = (SceneDelegate *)windowScene.delegate;
//            //       UIWindow * mainWindow = delegate.window;
//            //由于在sdk开发中，引入不了scenedelegate的头文件，所以需要使用KVC获取宿主app的window
//            UIWindow *mainWindow = [windowScene valueForKey:@"delegate.window"];
//            if(mainWindow){
//                return mainWindow;
//            }else{
//                return [UIApplication sharedApplication].windows.lastObject;
//            }
//        }else{
//            return [UIApplication sharedApplication].keyWindow;
//        }
//
//    }
//}

@end
